package com.itycu.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import io.swagger.annotations.*;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itycu.server.annotation.Log;
import com.itycu.server.core.controller.BaseController;
import com.itycu.server.core.domain.AjaxResult;
import com.itycu.server.enums.BusinessType;
import com.itycu.server.domain.system.SysUserRole;
import com.itycu.server.dto.system.SysUserRoleDto;
import com.itycu.server.service.system.impl.SysUserRoleService;
import com.itycu.server.utils.poi.ExcelUtil;
import com.itycu.server.core.page.TableDataInfo;

/**
 * 用户和角色关联Controller
 *
 * @author itycu
 * @date 2022-05-26
 */
@Api(value = "用户和角色关联",tags = {"用户和角色关联接口"})
@RestController
@RequestMapping("/system/userRole")
public class SysUserRoleController extends BaseController {
    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 查询用户和角色关联列表
     */
    @ApiOperation("查询用户和角色关联列表")
//    @PreAuthorize("@ss.hasPermi('server:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserRole sysUserRole) {
        startPage();
        List<SysUserRoleDto> list = sysUserRoleService.list(sysUserRole);
        return getDataTable(list);
    }

    /**
     * 用户和角色关联保存，id有值时更新，无值时新增
     */
    @ApiOperation(value = "用户和角色关联保存，id有值时更新，无值时新增")
    @PreAuthorize("@ss.hasPermi('server:role:save')")
    @Log(title = "用户和角色关联", businessType = BusinessType.SAVE)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody SysUserRole sysUserRole) {
        return toAjax(sysUserRoleService.save(sysUserRole), sysUserRole);
    }

    /**
     * 删除用户和角色关联
     */
    @ApiOperation("根据ID删除用户和角色关联")
    @PreAuthorize("@ss.hasPermi('server:role:remove')")
    @Log(title = "用户和角色关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{userId}")
    public AjaxResult remove(@PathVariable Long userId) {
        return toAjax(sysUserRoleService.deleteById(userId));
    }

    /**
     * 批量删除用户和角色关联
     */
    @ApiOperation("根据ID数组批量删除用户和角色关联")
    @PreAuthorize("@ss.hasPermi('server:role:remove')")
    @Log(title = "用户和角色关联", businessType = BusinessType.BATCHDEL)
    @DeleteMapping("/batchDel")
    public AjaxResult batchDel(@RequestBody @ApiParam(value = "id数组", required = true) List<Long> ids) {
        int count = 0;
        if (!CollectionUtils.isEmpty(ids)) {
            count = sysUserRoleService.deleteByIds(ids);
        }
        return toAjax(count);
    }

    /**
     * 根据id获取用户和角色关联详细信息
     */
    @ApiOperation("根据id获取用户和角色关联详细信息")
    @PreAuthorize("@ss.hasPermi('server:role:query')")
    @GetMapping("/findById/{userId}")
    public AjaxResult findById(@PathVariable("userId") Long userId) {
        return AjaxResult.success(sysUserRoleService.findById(userId));
    }

    /**
     * 导出用户和角色关联列表
     */
    @ApiOperation("导出用户和角色关联列表")
    @PreAuthorize("@ss.hasPermi('server:role:export')")
    @Log(title = "用户和角色关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserRole sysUserRole) {
        List<SysUserRoleDto> list = sysUserRoleService.list(sysUserRole);
        ExcelUtil<SysUserRoleDto> util = new ExcelUtil<SysUserRoleDto>(SysUserRoleDto.class);
        util.exportExcel(response, list, "用户和角色关联数据");
    }
}
